import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/login"
import Welcome from "@/views/welcome"
import Admin from "@/views/admin"
import Users from "@/views/useradmin/users"
import Roles from "@/views/rightsadmin/roles"
import Rights from "@/views/rightsadmin/rights"


import goods from "@/views/goodsadmin/goods"
import params from "@/views/goodsadmin/params"
import categories from "@/views/goodsadmin/categories"
import add from "@/views/goodsadmin/add"

import order from "@/views/ordersadmin/order"

import reports from "@/views/reportsadmin/reports"
Vue.use(VueRouter)

const routes = [
  {path:"/login",component:Login},
  {path:"/",redirect:"/login"},
  {
    path:"/admin",component:Admin,
    redirect:"/welcome",
    children:[
      {
        path:"/users",
        component:Users
      },
      {
        path:"/roles",
        component:Roles
      },
      {
        path:"/welcome",
        component:Welcome
      },
      {
        path:"/rights",
        component:Rights
      },
      {
        path:"/goods",
        component:goods,
      },
      {
        path:"/params",
        component:params
      },
      {
        path:"/categories",
        component:categories
      },
      {
        path:"/goods/add",
        component:add
      },
      {
        path:"/orders",
        component:order
      },
      {
        path:"/reports",
        component:reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
//对路由进行限制
router.beforeEach((to,from,next)=>{
  if(to.path === "/login") next();
  // 监测是否有token,如果没有则返回login页面
  let userToken = window.sessionStorage.getItem("usertoken")
  if(userToken){
    next()
  }else{
    next("/login")
  }
  
})
export default router
